import {get,post} from "../js/request"
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: () => import('../views/YvHome.vue')
    },
    {
      path: '/game',
      name: 'Game',
      component: () => import('../views/game/index.vue')
    },
    {
      path: '/game/play',
      name: 'GamePlay',
      component: () => import('../views/game/gameIframe.vue')
    },
    {
      path: '/game/:keyw',
      name: 'GameKey',
      redirect: to => {
        return { path: '/game/play', query: { keyw: to.params.keyw, hideTop: true, hideBottom: true } }
      }
    },
    {
      path: '/gameback/:keyw',
      name: 'GameBack',
      component: () => import('../views/game/gameGM.vue')
    },
    {
      path: '/websiteinfo',
      name: 'WebsiteInfo',
      component: () => import('../views/public/websiteinfo.vue')
    },
    {
      path: '/game/autoauth',
      name: 'GameAutoAuth',
      component: () => import('../views/game/custom/autoauth.vue')
    },
    {
      path: '/notFound',
      name: 'notFound',
      component: () => import('../views/public/notfound.vue')
    },
    // 文章
    {
      path: '/article/editor',
      name: 'ArticleEditor',
      component: () => import('../views/article/editor/index.vue')
    },
    {
      path: '/:pathMatch(.*)',
      redirect: to => {
        return { path: '/notFound', query: { from: to.href } }
      }
    },
    {
      path: '/tools/colorPalette',
      component: () => import("../views/tools/colorPalette/index.vue")
    },
    {
      path: '/tools/musicPlayer',
      component: () => import("../views/tools/musicPlayer/index.vue")
    },
    {
      path: '/tools/parseScale',
      component: () => import("../views/tools/parseScale/index.vue")
    }
  ]
})

var interval = null;

const NotVerify = ['/','/notFound','/websiteinfo','/redirect']

router.beforeEach((to,from,next)=>{
  if(NotVerify.find(e => e == to.path) != undefined){
    next()
    return;
  }
  post("/verify/simple",{   
    to: to.path,
    from: from.path,  
  }).then(res => {
    console.log(res)
    if(res.result.to == to.path && res.result.from == from.path){ 
      console.log(`%c 验证成功 - [/verify/simple] - ${new Date()}`, "background: #46e5f1; color;font-size: 8pt; padding: 2pt" ,res)
      next();
    }else {
      console.log(`%c 验证失败 - [/verify/simple] - ${new Date()}`, "background: #46e5f1; color;font-size: 8pt; padding: 2pt")
      redirect()
    }
  }).catch(res=>{
    alert(`验证出错 - ${new Date()} - ERROR [${res}]`, "background: #46e5f1; color;font-size: 8pt; padding: 2pt")
    console.log(`%c 验证出错 - ${new Date()} - ERROR [${res}]`, "background: #46e5f1; color;font-size: 8pt; padding: 2pt")
  })
})

export default router
